home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / signal.arc / SIGNAL.C < prev    next >
Encoding:
Text File  |  1985-07-26  |  6.6 KB  |  129 lines

  1. /*    Interrupt handler support for Lattice C Version 2.1x */
  2. /*
  3.         Author: Cheyenne Wills
  4.         Addr:   12 West Locust St.
  5.                         Mechanicsburg Pa. 17055
  6.  
  7.         Compuserv [70376,555]
  8.  
  9.         December 1984
  10.  
  11. Permission is granted to use, distribute and/or modify this code unless
  12. done for direct commercial profit.
  13. Author's name, address and this notice must be included in any copies.
  14.  
  15. No guarantees or warranties of any kind: This code is distributed
  16. "AS IS" without any warranty.  You are soley responsible for the
  17. selection of the program to achieve your intended results and for the
  18. results actually obtained.
  19.  
  20. Any suggestions for improvements gratefully accepted
  21.  
  22. */
  23.  
  24. /* Compile with the -s option of the Lattice C compiler. */
  25. /* This is required because of pointer arithmetic in calling */
  26. /* an assembler subroutine.     */
  27. /**
  28. *
  29. * name  signal -- Initializes an interrupt to be processed by a
  30. *                               C function.
  31. * synopsis      s =     signal(func,stacksize,intnum);
  32. *               type (*func)()     - address of function to handle interrupt
  33. *               unsigned stacksize - Size of stack to be used by interrupt handler
  34. *               unsigned intnum    - Interrupt number to process.
  35. *               char *s            - Pointer to the begining of the stack area
  36. * description   This function installs a C function as an interrupt
  37. *               handler.  This is done by obtaining a small amount of
  38. *               free storage as a stack and an interrupt boot strap.
  39. *               The size of the stack is set by the caller.  A couple of
  40. *               extra bytes are added at the end of this stack area for
  41. *               use of the interrupt boot strap.
  42. *               The Interrupt boot strap is a small amount of assembly code
  43. *               that calls a common assembly routine that establishes the
  44. *               environment for C.
  45. *               The following is the interrupt boot strap code:
  46. *       +---------------------------------------------------------------------------+
  47. *       |       Stack   Db      Stacksize dup(?)     ;Variable sized stack          |
  48. *       |       XCINTBOO Proc   Far                                                 |
  49. *       |               Call    Far     XCINTCOM     ;Call Common code              |
  50. *       |               Iret     P                                                  |
  51. *       |       FuncIp          Dw      Offset Func  ;Offset of C function to call  |
  52. *       |       FuncCs          Dw      Segment Func ;Segment of C function to call |
  53. *       |       FuncSs          Dw      ?            ;Stack segment of function     |
  54. *       |       FuncDs          Dw      ?            ;Data segment of function      |
  55. *       |       XCINTBOO Endp                                                       |
  56. *       +---------------------------------------------------------------------------+
  57. *               XCINTCOM establishes the C enviroment and calls the actual
  58. *               C function (XCINTCOM is a non public entry point).
  59. *
  60. *               There are no args to the called C function, but the following
  61. *               method can be used to obtain information about the
  62. *               interrupt.
  63. *               Take the address returned from SIGNAL add the size of the
  64. *               stack and subtract 8 words (16 bytes) this will be the
  65. *               address of the structure intinfo.  If you notice, not all
  66. *               of the registers are in this structure.  The rest can be
  67. *               found by taking data from Ss and Sp and using this as a
  68. *               pointer (segment/offset) to the structure intinfo2.
  69. *
  70. *               struct intinfo {
  71. *                       short   Di;     /* Di prior     to interrupt */
  72. *                       short   Si;     /* Si prior     to interrupt */
  73. *                       short   Dx;     /* Dx prior     to interrupt */
  74. *                       short   Cx;     /* Cx prior     to interrupt */
  75. *                       short   Bx;     /* Bx prior     to interrupt */
  76. *                       short   Ax;     /* Ax prior     to interrupt */
  77. *                       short   Sp;     /* Sp prior     to interrupt (see note) */
  78. *                       short   Ss;     /* Ss prior     to interrupt */
  79. *               /* The following defines the interrupt boot code and should */
  80. *               /* not be modified */
  81. *                       char    FCALL[5];       /* Call to XCINTCOM     */
  82. *                       char    IRET;           /* Iret instruction     */
  83. *                       short   FUNCIP;         /* IP of function */
  84. *                       short   FUNCCS;         /* CS of function */
  85. *                       short   FUNCDS;         /* DS of function */
  86. *                       short   FUNCSS;         /* SS of function */
  87. *       };
  88. *
  89. *       Note: By using the data obtained from intinfo and Ss and Sp
  90. *             you can obtain access to the following information:
  91. *
  92. *               struct intinfo2 {
  93. *                       short Bx;        /* Bx prior to interrupt */
  94. *                       short Ds;        /* Ds prior to interrupt */
  95. *                       short Es;        /* Es prior to interrupt */
  96. *                       short Bp;        /* Bp prior to interrupt */
  97. *                       short Ip;        /* Ip prior to interrupt    <--+  Hardware   */
  98. *                       short Cs;        /* Cs prior to interrupt       += generated  */
  99. *                       short Flags;     /* Flags prior to interrupt <--+  during int */
  100. *               /* Anything following here belongs to the task that was interrupted */
  101. *       }
  102. *
  103. *       cautions:
  104. *               It is the responsibility of the calling function to
  105. *               save the current interrupt routines address, and to restore it.
  106. *               It is also the responsibility of the calling function to
  107. *               return the storage when its done.  Take the original stack
  108. *               size and add SIZE_OF_IBC then issue a rlsmem
  109. *
  110. *               The minimum size of the stack is around 24 bytes.
  111. *
  112. **/
  113.  
  114. #include <signal.h>
  115. extern char     *getmem();
  116. char            *signal(func,stack,vecno)
  117. int             (*func)();      /* function which will process interrupt */
  118. unsigned stack;                 /* # of bytes of stack needed by function */
  119. unsigned vecno;                 /* # of vector for interrupt trap */
  120. {
  121.   char *ustack;
  122.   char *ibc;
  123.  
  124.   ustack=getmem(stack+SIZE_OF_IBC);      /* this is where interrupt begins */
  125.   ibc=ustack+stack;
  126.   XCINTRIN(vecno,func,ibc);
  127.   return ustack;
  128. }
  129.